<?php

class Model_Orm_Search_Tests extends \PHPUnit_Framework_TestCase
{
    public static function setupBeforeClass()
    {
        $movie1 = \Model_Orm_Movie::forge();
        $movie1->title_lv = 'Sabotāža';
        $movie1->title_en = 'Sabotage';
        $movie1->title_original = 'Sabotage';
        $movie1->year = 2014;
        $movie1->description_lv = 'ASV narkotiku apkarošanas pārvaldes elites vienība darbojas nevainojami. Tajā dien labākie no labākajiem. Līderis Džons Vortons (A. Švarcenegers) nav guvis cieņu ar tukšu salmu kulšanu. Viņš uzticas savējiem, un viņi uzticas cits citam. Vienība savā ziņā kļuvusi par ģimeni. Tā arī turpinātos, ja kāda operācija nebūtu visu sabojājusi.';
        $movie1->description_en = 'Members of an elite DEA task force find themselves being taken down one by one after they rob a drug cartel safe house.';
        $movie1->video_id = 'FeHpSdQSH0c';
        $movie1->duration = 109;
        $movie1->age_restriction = 16;
        $movie1->poster_large = 'poster_sabotage.jpg';
        $movie1->poster_small = 'small_sabotage.jpg';
        $movie1->save();

        $movie2 = \Model_Orm_Movie::forge();
        $movie2->title_lv = 'Kapteinis Amerika: Ziemas kareivis';
        $movie2->title_en = 'Captain America: The Winter Soldier';
        $movie2->title_original = 'Captain America: The Winter Soldier';
        $movie2->year = 2014;
        $movie2->description_lv = 'Pēc notikumiem, kas pirmo reizi apvienoja Atriebēju komandu, Stīvs Rodžerss, pazīstams arī kā kapteinis Amerika, apmetas Vašingtonā un cenšas pielāgoties dzīvei mūsdienu pasaulē. Taču par mierīgu dzīvi viņš var tikai pasapņot. Cenšoties palīdzēt kolēģim no S.H.I.E.L.D. , Stīvs tiek ierauts notikumos, kas draud izvērsties par globāla mēroga katastrofu. Lai atklātu šo sazvērestību, kapteinis Amerika apvieno spēkus ar Melno atraitni. Viņiem pievienojas Piekūns, un drīz vien viņi sastopas ar savu jauno ienaidnieku – Ziemas kareivi.';
        $movie2->description_en = 'Steve Rogers struggles to embrace his role in the modern world and battles a new threat from old history: the Soviet agent known as the Winter Soldier.';
        $movie2->video_id = 'NLWsK1ZFunA';
        $movie2->duration = 136;
        $movie2->age_restriction = 13;
        $movie2->poster_large = 'poster_captamerica.jpg';
        $movie2->poster_small = 'small_captamerica.jpg';
        $movie2->save();

        $movie3 = \Model_Orm_Movie::forge();
        $movie3->title_lv = 'Rio 2';
        $movie3->title_en = 'Rio 2';
        $movie3->title_original = 'Rio 2';
        $movie3->year = 2014;
        $movie3->description_lv = 'Blū un Pērle pavada laiku aprūpējot savus trīs bērneļus un mīlošais tēvs, protams, nav ieinteresēts pamest savu drošo un mājīgo dzīvesvietu pasakainajā Riodežaneiro. Taču Pērle uzskata, ka ir pienācis laiks, lai bērni mācītos kļūt par īstiem putniem. Un tā, visa ģimene dodas ceļojumā uz Amazones mežonīgajiem džungļiem, kur, tāpat kā burvīgajā Rio, valda ugunīgi ritmi, krāšņa daba un kur katra dienu nes sev līdzi elpu aizraujošus piedzīvojumus.';
        $movie3->description_en = 'It\'s a jungle out there for Blu, Jewel and their three kids after they\'re hurtled Rio de Janeiro to the wilds of the Amazon. As Blu tries to fit in, he goes beak-to-beak with the vengeful Nigel, and meets the most fearsome adversary of all: his father-in-law.';
        $movie3->video_id = 'IkZM1Zc0mBU';
        $movie3->duration = 101;
        $movie3->age_restriction = 0;
        $movie3->poster_large = 'poster_rio2.jpg';
        $movie3->poster_small = 'small_rio2.jpg';
        $movie3->save();

        $movie4 = \Model_Orm_Movie::forge();
        $movie4->title_lv = 'Sliktie kaimiņi';
        $movie4->title_en = 'Bad Neighbors';
        $movie4->title_original = 'Bad Neighbors';
        $movie4->year = 2014;
        $movie4->description_lv = 'Kādā saulainā dienā klusā piepilsētā kaimiņos jaunai ģimenei ievācās studentu brālība... Un ja kādam šķiet, ka viņš spēj prognozēt turpmāko notikumu attīstības gaitu, kas tāds nebūs pat sapņos rādījies!';
        $movie4->description_en = 'A couple with a newborn baby face unexpected difficulties after they are forced to live next to a fraternity house.';
        $movie4->video_id = 'vP9QCAcGy7Y';
        $movie4->duration = 96;
        $movie4->age_restriction = 16;
        $movie4->poster_large = 'poster_badneighbors.jpg';
        $movie4->poster_small = 'small_badneighbors.jpg';
        $movie4->save();

        $movie5 = \Model_Orm_Movie::forge();
        $movie5->title_lv = 'Hobits: Smoga posta zeme';
        $movie5->title_en = 'The Hobbit: The Desolation of Smaug';
        $movie5->title_original = 'The Hobbit: The Desolation of Smaug';
        $movie5->year = 2014;
        $movie5->description_lv = 'Negaidītā ceļojuma sākums jau ir aiz muguras, un draugi dodas uz Austrumiem, ceļā sastopoties ar ādu mainītāju Beornu un milzu zirnekļu baru. Pēc izglābušanās no bīstamo Drūmmeža elfu gūsta, tālāk rūķu ceļš ved uz Ezera pilsētu un visbeidzot uz pašu Vientuļo kalnu. Tur viņus sagaida lielākas briesmas – šausminošākā no visām radībām, kas pārbaudīs ne tikai viņu drosmi, bet arī draudzību un gudrību, dodoties šajā ceļojumā, – pūķis Smogs.';
        $movie5->description_en = 'The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring.';
        $movie5->video_id = 'fnaojlfdUbs';
        $movie5->duration = 161;
        $movie5->age_restriction = 13;
        $movie5->poster_large = 'poster_hobbit.jpg';
        $movie5->poster_small = 'small_hobbit.jpg';
        $movie5->save();

        $movie6 = \Model_Orm_Movie::forge();
        $movie6->title_lv = 'Atriebība sieviešu gaumē';
        $movie6->title_en = 'The Other Woman';
        $movie6->title_original = 'The Other Woman';
        $movie6->year = 2014;
        $movie6->description_lv = 'Seksīgā blondīne Kerlija, kurai ir burvīgas attiecības ar savu jauno draugu Marku, nejauši satiek ... Marka sievu un saprot, ka viņas lieliskais vīrietis ir banāls brunču mednieks. Apkrāptās sievietes nolemj neskaidrot savstarpējās attiecības, bet palīdzēt viena otrai un apvienot spēkus, lai pārmācītu nodevēju. Taču jaunizceptās draudzenes pēkšņi atklāj, ka Markam ir vēl viena sieviete. Trīs liktenīgas daiļavas uz kara ceļa - atriebība sieviešu gaumē sola lielas nepatikšanas viņu vīrietim.';
        $movie6->description_en = 'After discovering her boyfriend is married, Carly soon meets the wife he\'s been cheating on. And when yet another affair is discovered, all three women team up to plot mutual revenge on the three-timing SOB.';
        $movie6->video_id = 'oDcaZ3StTfI';
        $movie6->duration = 109;
        $movie6->age_restriction = 13;
        $movie6->poster_large = 'poster_theotherwoman.jpg';
        $movie6->poster_small = 'small_theotherwoman.jpg';
        $movie6->save();
        
        $genre = \Model_Orm_Genre::forge();     //1
        $genre->genre_lv = "Asa sižeta";
        $genre->genre_en = "Action";
        $genre->save();	

        $genre2 = \Model_Orm_Genre::forge();    //2
        $genre2->genre_lv = "Šausmu filmas";
        $genre2->genre_en = "Horror movies";
        $genre2->save();	

        $genre3 = \Model_Orm_Genre::forge();    //3
        $genre3->genre_lv = "Dokumentālās filmas";
        $genre3->genre_en = "Documentary";
        $genre3->save();	

        $genre4 = \Model_Orm_Genre::forge();    //4
        $genre4->genre_lv = "Zinātniskā fantastika";
        $genre4->genre_en = "Science fiction";
        $genre4->save();	

        $genre5 = \Model_Orm_Genre::forge();    //5
        $genre5->genre_lv = "Detektīvfilmas";
        $genre5->genre_en = "Detective movies";
        $genre5->save();

        $genre6 = \Model_Orm_Genre::forge();    //6
        $genre6->genre_lv = "Piedzīvojumu filmas";
        $genre6->genre_en = "Adventure movies";
        $genre6->save();

        $genre7 = \Model_Orm_Genre::forge();    //7
        $genre7->genre_lv = "Multfilmas";
        $genre7->genre_en = "Cartoons";
        $genre7->save();

        $genre8 = \Model_Orm_Genre::forge();    //8
        $genre8->genre_lv = "Komēdijas";
        $genre8->genre_en = "Comedy";
        $genre8->save();
        
        $movie_genres = array(
            1 => array(1),
            2 => array(1,6,4),
            3 => array(6,7,8),
            4 => array(8),
            5 => array(6),
            6 => array(8)
        );

        foreach($movie_genres as $movie_id => $movie_genre_ids) {
            foreach($movie_genre_ids as $genre_id) {
                $moviegenre = \Model_Orm_Moviegenre::forge();
                $moviegenre->movie_id = $movie_id;
                $moviegenre->genre_id = $genre_id;
                $moviegenre->save();
            }
        }
        
        $auditorium1 = \Model_Orm_Auditorium::forge();
        $auditorium1->auditorium_name = '1. auditorija';
        $auditorium1->rows = 5;
        $auditorium1->seats_per_row = 10;
        $auditorium1->support_3d = 0;
        $auditorium1->save();

        $auditorium2 = \Model_Orm_Auditorium::forge();
        $auditorium2->auditorium_name = '2. auditorija';
        $auditorium2->rows = 10;
        $auditorium2->seats_per_row = 15;
        $auditorium2->support_3d = 1;
        $auditorium2->save();

        $auditorium3 = \Model_Orm_Auditorium::forge();
        $auditorium3->auditorium_name = '3. bērnu filmu auditorija';
        $auditorium3->rows = 7;
        $auditorium3->seats_per_row = 10;
        $auditorium3->support_3d = 1;
        $auditorium3->save();
        
        $all_shows = array(
            array('movie_id' => 1, 'auditorium_id' => 1, 'start_time' => 1402056000, 'end_time' => 1402063200, 'price' => 4.99),
            array('movie_id' => 2, 'auditorium_id' => 2, 'start_time' => 1402066800, 'end_time' => 1402074000, 'price' => 5.99),
            array('movie_id' => 3, 'auditorium_id' => 3, 'start_time' => 1402142400, 'end_time' => 1402149600, 'price' => 7.00),
            array('movie_id' => 4, 'auditorium_id' => 2, 'start_time' => 1402142400, 'end_time' => 1402149600, 'price' => 7.00),
            array('movie_id' => 5, 'auditorium_id' => 1, 'start_time' => 1402311600, 'end_time' => 1402326000, 'price' => 7.50),
            array('movie_id' => 6, 'auditorium_id' => 1, 'start_time' => 1402142400, 'end_time' => 1402149600, 'price' => 7.00),
            array('movie_id' => 1, 'auditorium_id' => 2, 'start_time' => 1402329600, 'end_time' => 1402340400, 'price' => 7.00),
            array('movie_id' => 2, 'auditorium_id' => 1, 'start_time' => 1402246800, 'end_time' => 1402254000, 'price' => 6.00),
            array('movie_id' => 3, 'auditorium_id' => 1, 'start_time' => 1402142400, 'end_time' => 1402149600, 'price' => 6.50)
        );

        foreach($all_shows as $data) {
            $show = \Model_Orm_Show::forge();
            $show->movie_id = $data['movie_id'];
            $show->auditorium_id = $data['auditorium_id'];
            $show->start_time = $data['start_time'];
            $show->end_time = $data['end_time'];
            $show->price = $data['price'];
            $show->save();
        }
    }
  
    /*
        $results = DB::query('    
        SELECT DISTINCT shows.id FROM shows
        INNER JOIN movies ON movies.id = shows.movie_id
        INNER JOIN movie_genres ON movie_genres.movie_id = movies.id
        WHERE shows.start_time BETWEEN '. $start_timestamp .' AND '.$end_timestamp.
            ((int)Input::get('genre') > 0 ? (' AND movie_genres.genre_id = ' . Input::get('genre')) : '') .
            ' AND     (movies.title_lv LIKE \'%'. Input::get('title') .'%\'
                    OR movies.title_en LIKE \'%'. Input::get('title') .'%\'
                    OR movies.title_original LIKE \'%'. Input::get('title') .'%\')              
        '
        )->execute();    
    
     */
    public function test_src_1() {
        $start_timestamp = 1402002000;
        $end_timestamp = 1402088400;

        $results = DB::query('    
        SELECT DISTINCT shows.id FROM shows
        WHERE shows.start_time BETWEEN '. $start_timestamp .' AND '.$end_timestamp               
        )->execute();

        if(count($results) <= 0) {
            throw new Exception('Movie not found !');
        }
    }
    
    public function test_src_2() {
        $results = DB::query('    
            SELECT DISTINCT shows.id
            FROM shows
            INNER JOIN movies ON movies.id = shows.movie_id
            WHERE movies.title_lv LIKE \'%hobits%\''
        )->execute()->count();

        if(count($results) == 0) {
            throw new Exception('Movie not found ! tottal: '. count($results));
        }
    }
    
    public function test_src_3() {
        $results = DB::query('    
        SELECT DISTINCT shows.id FROM shows
        INNER JOIN movie_genres ON movie_genres.movie_id = shows.movie_id
        WHERE movie_genres.genre_id = 8'             
        )->execute()->count();

        if(count($results) <= 0) {
            throw new Exception('Movie not found !');
        }
    }
    
    public function test_src_4() {
        $results = DB::query('    
        SELECT DISTINCT shows.id FROM shows
        INNER JOIN movies ON movies.id = shows.movie_id
        INNER JOIN movie_genres ON movie_genres.movie_id = movies.id
        WHERE shows.start_time BETWEEN 1402002000 AND 1402088400'.
            ' AND movie_genres.genre_id = 1'.
            ' AND     movies.title_lv LIKE \'%sabotāža%\'           
        '
        )->execute()->count();

        if(count($results) == 0) {
            throw new Exception('Movie not found !');
        }
    }
    
    public function test_src_5() {
        $results = DB::query('    
        SELECT DISTINCT shows.id FROM shows
        INNER JOIN movies ON movies.id = shows.movie_id
        INNER JOIN movie_genres ON movie_genres.movie_id = movies.id
        WHERE shows.start_time BETWEEN 1401001000 AND 1401088400'.
            ' AND movie_genres.genre_id = 1'.
            ' AND     movies.title_lv LIKE \'%sabotāža%\'          
        '
        )->execute()->count();

        if(count($results) < 1) {
            throw new Exception('Invalid shows found !');
        }
    }
    
    public function test_src_6() {
        $results = DB::query('    
        SELECT DISTINCT shows.id FROM shows
        INNER JOIN movies ON movies.id = shows.movie_id
            WHERE movies.title_lv LIKE \'%sabotāža%\'           
        '
        )->execute()->count();

        if(count($results) <= 0) {
            throw new Exception('Movie not found !');
        }
    }
    
    public function test_src_7() {
        $results = DB::query('    
        SELECT DISTINCT shows.id FROM shows
        INNER JOIN movies ON movies.id = shows.movie_id
            WHERE movies.title_en LIKE \'%sabotage%\'           
        '
        )->execute()->count();

        if(count($results) <= 0) {
            throw new Exception('Movie not found !');
        }
    }
    
    public function test_src_8() {
        $results = DB::query('    
        SELECT DISTINCT shows.id FROM shows
        INNER JOIN movies ON movies.id = shows.movie_id
            WHERE movies.title_original LIKE \'%sabotage%\'           
        '
        )->execute()->count();

        if(count($results) <= 0) {
            throw new Exception('Movie not found !');
        }
    }
    
    public function test_src_9() {
        $results = DB::query('    
        SELECT DISTINCT shows.id FROM shows
        INNER JOIN movies ON movies.id = shows.movie_id
            WHERE (movies.title_lv LIKE \'%Zvaigžņu kari%\'
                    OR movies.title_en LIKE \'%Star Wars%\'
                    OR movies.title_original LIKE \'%Star Wars%\')          
        '
        )->execute()->count();

        if(count($results) < 1) {
            throw new Exception('Invalid movies found ! ' . count($results));
        }
    }
}